/*
 *  Copyright 2009 Ancora Research Group.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

/*
 * MainWindow.java
 *
 * Created on 25/Set/2009, 18:01:07
 */

package pt.ualg.carr.gui;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import pt.ualg.Car.common.GuiUtils;
import pt.ualg.carr.client1.Command;
import pt.ualg.carr.client1.CommandListener;

/**
 *
 * @author Ancora Group <ancora.codigo@gmail.com>
 */
public class MainWindow extends javax.swing.JFrame implements CommandListener {

    /** Creates new form MainWindow */
    public MainWindow() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
   // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
   private void initComponents() {

      jTextField1 = new javax.swing.JTextField();
      jTextField2 = new javax.swing.JTextField();
      jTextField3 = new javax.swing.JTextField();
      jTextField4 = new javax.swing.JTextField();
      jTextField5 = new javax.swing.JTextField();
      jTextField6 = new javax.swing.JTextField();
      jLabel1 = new javax.swing.JLabel();
      jLabel2 = new javax.swing.JLabel();
      jLabel3 = new javax.swing.JLabel();
      jLabel4 = new javax.swing.JLabel();
      jLabel5 = new javax.swing.JLabel();
      jLabel6 = new javax.swing.JLabel();
      jButton1 = new javax.swing.JButton();

      setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
      addKeyListener(new java.awt.event.KeyAdapter() {
         public void keyTyped(java.awt.event.KeyEvent evt) {
            formKeyTyped(evt);
         }
      });

      jTextField1.setEditable(false);

      jTextField2.setEditable(false);

      jTextField3.setEditable(false);

      jTextField4.setEditable(false);

      jTextField5.setEditable(false);

      jTextField6.setEditable(false);

      jLabel1.setText("Port 1");

      jLabel2.setText("Port 2");

      jLabel3.setText("Port 3");

      jLabel4.setText("Port 4");

      jLabel5.setText("Port 5");

      jLabel6.setText("Port 6");

      jButton1.setText("jButton1");
      jButton1.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton1ActionPerformed(evt);
         }
      });

      javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
      getContentPane().setLayout(layout);
      layout.setHorizontalGroup(
         layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
               .addComponent(jTextField6, javax.swing.GroupLayout.Alignment.TRAILING)
               .addComponent(jTextField5, javax.swing.GroupLayout.Alignment.TRAILING)
               .addComponent(jTextField4, javax.swing.GroupLayout.Alignment.TRAILING)
               .addComponent(jTextField3, javax.swing.GroupLayout.Alignment.TRAILING)
               .addComponent(jTextField2, javax.swing.GroupLayout.Alignment.TRAILING)
               .addComponent(jTextField1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 42, Short.MAX_VALUE))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
               .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                           .addGroup(layout.createSequentialGroup()
                              .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                              .addComponent(jLabel1))
                           .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                              .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                              .addComponent(jLabel2)))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                           .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                           .addComponent(jLabel3)))
                     .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel4)))
                  .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                     .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                     .addComponent(jLabel5)))
               .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                  .addGap(6, 6, 6)
                  .addComponent(jLabel6)))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 195, Short.MAX_VALUE)
            .addComponent(jButton1)
            .addGap(43, 43, 43))
      );
      layout.setVerticalGroup(
         layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
               .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
               .addComponent(jLabel1)
               .addComponent(jButton1))
            .addGap(28, 28, 28)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
               .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
               .addComponent(jLabel2))
            .addGap(31, 31, 31)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
               .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
               .addComponent(jLabel3))
            .addGap(34, 34, 34)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
               .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
               .addComponent(jLabel4))
            .addGap(31, 31, 31)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
               .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
               .addComponent(jLabel5))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 23, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
               .addComponent(jTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
               .addComponent(jLabel6))
            .addGap(19, 19, 19))
      );

      pack();
   }// </editor-fold>//GEN-END:initComponents

    private void formKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_formKeyTyped
       // TODO add your handling code here:
       System.out.println("Typed");
    }//GEN-LAST:event_formKeyTyped

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
       // TODO add your handling code here:
    }//GEN-LAST:event_jButton1ActionPerformed

    /**
    * @param args the command line arguments
    */

    /*
    public static JFrame createFrame() {
       JFrame mainWindow = new MainWindow();
    }
     */

    public static void main(String[] args) {
       // Anonymous Class
       GuiUtils.runOnEdt(new Runnable() {
         @Override
            public void run() {
                new MainWindow().setVisible(true);               
            }
       });

      /*
      try {
         Thread.sleep(100000);
      } catch (InterruptedException ex) {
         Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
      }
       */
    }

    /*
    public static JFrame startGui() {
       // Anonymous Class
       Runnable windowRunnable = new Runnable() {
         @Override
            public void run() {
                mainWindow = new MainWindow();
                mainWindow.setVisible(true);
            }

         public JFrame getMainWindow() {
            return mainWindow;
         }

         // INSTANCE VARIABLES
         public JFrame mainWindow;
        };

        java.awt.EventQueue.invokeLater(windowRunnable);
        return windowRunnable.mainWindow;
    }
     */
     

   // Variables declaration - do not modify//GEN-BEGIN:variables
   private javax.swing.JButton jButton1;
   private javax.swing.JLabel jLabel1;
   private javax.swing.JLabel jLabel2;
   private javax.swing.JLabel jLabel3;
   private javax.swing.JLabel jLabel4;
   private javax.swing.JLabel jLabel5;
   private javax.swing.JLabel jLabel6;
   private javax.swing.JTextField jTextField1;
   private javax.swing.JTextField jTextField2;
   private javax.swing.JTextField jTextField3;
   private javax.swing.JTextField jTextField4;
   private javax.swing.JTextField jTextField5;
   private javax.swing.JTextField jTextField6;
   // End of variables declaration//GEN-END:variables



   /**
    * Updates the values of the text labes. Is invoked in AWT queue.
    * 
    * @param command
    */
   @Override
   public void processCommand(final Command command) {

      java.awt.EventQueue.invokeLater(new Runnable() {
         @Override
            public void run() {
      jTextField1.setText(String.valueOf(command.getAngles()[0]));
      jTextField2.setText(String.valueOf(command.getAngles()[1]));
      jTextField3.setText(String.valueOf(command.getAngles()[2]));
      jTextField4.setText(String.valueOf(command.getAngles()[3]));
      jTextField5.setText(String.valueOf(command.getAngles()[4]));
      jTextField6.setText(String.valueOf(command.getAngles()[5]));
            }
        });  
   }

   /**
    * Updates the Text Fields
    * 
    * @param command
    */
   public void updateTextFields(Command command) {
      jTextField1.setText(String.valueOf(command.getAngles()[0]));
      jTextField2.setText(String.valueOf(command.getAngles()[1]));
      jTextField3.setText(String.valueOf(command.getAngles()[2]));
      jTextField4.setText(String.valueOf(command.getAngles()[3]));
      jTextField5.setText(String.valueOf(command.getAngles()[4]));
      jTextField6.setText(String.valueOf(command.getAngles()[5]));
   }


}
